import logging

from loginserver.lib.base import *
from indra.base.llsd import parse, format_xml

log = logging.getLogger(__name__)

MODULES = {
    'inventory' : 'http://mrtopf.de',
    'god' : 'http://mrtopf.de',
    'shutdown' : 'http://mrtopf.de',
}

def CAP2(data):
    """post to an url"""
    host = config['loginserver.capsserver.host']
    port = config['loginserver.capsserver.port']
    path = config['loginserver.capsserver.path']
    params = urllib.urlencode({'data': format_xml(data)})
    print params
    headers = {"Accept": "text/plain", 'Content-Type':'application/x-www-form-urlencoded'}

    conn = httplib.HTTPConnection(host,port)
    conn.request("POST", path, params, headers)
    r1 = conn.getresponse()
    resdata = r1.read()
    conn.close()
    return resdata


class SeedController(BaseController):

    def respond(self):
        """handle cap requests"""
        data = parse(request.POST['data'])
        wanted = data['caps-wanted']
        caps = {}
        for cap_request in wanted:
            caps['wanted'] = CAP2(MODULES['cap_request'])
        return format_xml(caps)
